package rush.rushcampus.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import rush.rushcampus.common.BaseContext;
import rush.rushcampus.common.Result;
import rush.rushcampus.dto.UserDto;
import rush.rushcampus.entity.User;
import rush.rushcampus.mapper.UserMapper;
import rush.rushcampus.service.IUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import rush.rushcampus.vo.UserInfoVo;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author rush
 * @since 2024-06-11
 */
@Slf4j
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {


    @Override
    public Result register(UserDto userDto) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("phone", userDto.getPhone());
        User user1 = this.getOne(queryWrapper);
        //先检测是否由用户存在
        if ( user1 != null) {
            return Result.error("用户已存在");
        }
        //填充其他字段
        User user = new User();
        BeanUtils.copyProperties(userDto,user);
        user.setCreateTime(LocalDateTime.now());
        user.setConcernTag("[]");
        user.setStarArticle("[]");
        user.setStatus(0);
        user.setGender(0);
        user.setUserIdentity(0);
        this.save(user);
        BaseContext.setCurrentId(user.getId());
        return Result.success("注册成功");
    }

    @Override
    public Result login (HttpServletRequest request , UserDto userDto) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("phone", userDto.getPhone());
        queryWrapper.eq("password", userDto.getPassword());
        User user1 = this.getOne(queryWrapper);
        if (user1 == null) {
            return Result.error("账号或密码错误");
        }

        //        登录成功，将其放入Session并返回的登陆成功结果
        HttpSession session = request.getSession();
        session.setAttribute("userInfo", user1);
        log.info(request.getSession().getAttribute("userInfo").toString());
        return Result.success("登录成功");
    }

    @Override
    public void concern(Long id) {
        //用户关注
        User user = this.getById(BaseContext.getCurrenId());
        String concernTag = user.getConcernTag();
        concernTag = concernTag.substring(0, concernTag.length() - 1);
        concernTag += "," + id + "]";
        user.setConcernTag(concernTag);
        this.updateById(user);
    }

    @Override
    public UserInfoVo getMyInfoVo() {
        User user = this.getById(1L);
        UserInfoVo userInfoVo = new UserInfoVo();
        BeanUtils.copyProperties(user,userInfoVo);
        int ConcernUser = user.getConcernUser() == null ? 0 : user.getConcernUser().length() - user.getConcernUser().replace(String.valueOf(",")+1, "").length();
        userInfoVo.setConcernUser(Long.valueOf(ConcernUser));
        int fans = user.getFans() == null ? 0 : user.getFans().length() - user.getFans().replace(String.valueOf(","), "").length()+1;
        userInfoVo.setFans(Long.valueOf(fans));
        return userInfoVo;
    }

    public void starArticle(Long id, Integer type , Integer isStar) {
        //1.查看star_article或concern_user字段是否为null(当前用户为1)
        User user = this.getById(1L);
        if (type == 1){
            if (user.getStarArticle() == null){
                user.setStarArticle(id.toString());
            }else {
                if (isStar == 0){
                    user.setStarArticle(user.getStarArticle().replace(id.toString(),""));
                }else if (isStar == 1){
                    user.setStarArticle(user.getStarArticle() + "," + id);
                }
            }
        }else {
            if (user.getConcernUser() == null){
                user.setConcernUser(id.toString());
            }else {
                if (isStar == 0){
                    user.setConcernUser(user.getConcernUser().replace(id.toString(),""));
                }else if (isStar == 1){
                    user.setConcernUser(user.getConcernUser() + "," + id);
                }
            }
        }
        this.updateById(user);
    }
}
